package com.fan.chat.component;

import com.plexpt.chatgpt.ChatGPTStream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ChatGPTStreamComponent {

    @Autowired
    private ChatConfig config;

    public ChatGPTStream getChatGPTStream() {
        ChatGPTStream chatGPTStream = getInitialChatGPTStream();
        chatGPTStream.setApiKeyList(config.getApiKey());
        return chatGPTStream.init();
    }

    public ChatGPTStream getChatGPTStream(String apiKey) {
        ChatGPTStream chatGPTStream = getInitialChatGPTStream();
        chatGPTStream.setApiKey(apiKey);
        return chatGPTStream.init();
    }

    public ChatGPTStream getFasterChatGPTStream() {
        ChatGPTStream fasterChatGPTStream = getInitialChatGPTStream();
        fasterChatGPTStream.setApiKeyList(config.getFasterApiKey());
        return fasterChatGPTStream.init();
    }

    private ChatGPTStream getInitialChatGPTStream() {
        ChatGPTStream chatGPTStream = ChatGPTStream.builder()
                .timeout(600)
                .apiHost(config.apiHost)
                .build();

        chatGPTStream.setProxy(config.getProxy());
        return chatGPTStream;
    }

}
